/**
 * Created by hucy on 2016-05-19.
 */
admin.controller('UsrListCtrl',[
	'$scope',
	'$location',
  'load2scope',
  'removeFromList',
  function($scope,$location,load2scope,removeFromList){

    $scope.showDetail = function () {
  		var id = $('[name="ids"]:checked').val();
      if(!id) return;
  		$location.path('/detail/usr/'+id);
  	};

    // 删除部门
    $scope.remove = function () {
      var option = {
        $scope : $scope,
        table  : 'usr',
        modalConfig : {
          title:'删除确认',
          content:'您确定要删除该项？<br />\
                  <strong>此操作不可逆！</strong>',
        }
      };
      removeFromList(option);
    }
	}
])
.controller('UsrDetailCtrl',[
	'$scope',
	'$uibModal',
  '$http',
	'$stateParams',
	'localStorageService',
	'load2scope',
  function($scope,$uibModal,$http,$stateParams,localStorageService,load2scope){
  	var id = $stateParams.id;
    load2scope(['usr' , 'dept' ,'role' ,'classes'] , $scope);

    $scope.dept.unshift({dept_name:'--请选择--',id:null});
    $scope.role.unshift({role_name:'--请选择--',id:null});
  	$scope.user = $scope.map.usr[id];
    $scope.tempUsr = {};
    $scope.tempBirth = parseInt($scope.user.usr_birth);
    $scope.update  = true;

    // 切换更新状态
    $scope.toggleUpdate = function () {
      if( $scope.update ){
        // 暂存
        $.extend($scope.tempUsr,$scope.user);
  	    $scope.tempBirth = $scope.tempUsrBirth.getTime();
  		}else{
  			// 取消就用暂存重置
  			$.extend($scope.user,$scope.tempUsr);
        $scope.tempUsrBirth = new Date( $scope.tempBirth );
  			if ($scope.setNewMenber){
  				$scope.setNewMenber = true;
  				$scope.user.usr_status = 1;
  			}
  		}
  		$scope.update = !$scope.update;
  	}

  	// 设置用户状态
  	$scope.setMenber = function (status) {
  		if(status === 2) {
  			$scope.update = false;
  			$scope.setNewMenber = true;
  		}else if(status === 1){
  			// 角色重置为普通学生
  			$scope.user.usr_role = null;
  			// 部门重置为空
  			$scope.user.usr_dept = null;
  			console.log($scope.user);
        $scope.modify();
  		}else if(status === 3){
  			var modalInstance = $uibModal.open({
		      // animation: $scope.animationsEnabled,
		      templateUrl: 'Modal.html',
		      controller: 'RetireModalCtrl' ,
		    });
		    modalInstance.result.then(function ( retire ) {
          // 点击确定会走这里
		      if(retire) {
            $scope.user.usr_status = status;
          }
		    }, function () {
		      // 点击取消会走这里
          console.log( '取消时间' + new Date().getTime() );
		    });
  		}
	 		status !== 3 && ($scope.user.usr_status = status);
  	}

  	// 日期选择器
  	$scope.dateOptions = {
	    dateDisabled: false,
	    formatDayTitle:'yyyy年 MM月',
	    maxDate: new Date(2020, 5, 22),
	    minDate: new Date(0),
	    startingDay: 0
	  };

	  $scope.tempUsrBirth = new Date( parseInt($scope.user.usr_birth) );
    // 修改用户信息
    $scope.modify = function () {

      $scope.user.usr_birth = $scope.tempUsrBirth.getTime();

      console.log( $scope.user );

      $http.put('/api/usr/'+$scope.user.id , $scope.user).then(function (data) {
        // console.log(data);
        show_stack('success2');
        $scope.toggleUpdate();

      }, function () {
        console.log('fail');
      });

    }
    // fake_load();
	}
])
.controller('RetireModalCtrl',['$scope','$uibModalInstance',function ($scope,$uibModalInstance) {
  $scope.modal = {
    title:'退役确认',
    content:'您确定要设置该成员为退役状态？<strong>设置之后您将不能更改任何信息！</strong>',
  };
  $scope.ok = function () {
    $uibModalInstance.close( true );
    show_stack('success');
  };

  $scope.cancel = function () {
    $uibModalInstance.dismiss('cancel');
  };
}])


function fake_load() {
    var cur_value = 1,
        progress;
        // stack_bottomright = {"dir1": "left", "dir2": "up", "push": "top"};
    PNotify.prototype.options.styling = "bootstrap3";
    // Make a loader.
    var loader = new PNotify({
        title: "进度条",
        text: '<div class="progress progress-striped active" style="margin:0">\
                  <div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0">\
                      <span class="sr-only">0%</span>\
                  </div>\
              </div>',
        // icon: 'fa fa-moon-o fa-spin',
        icon: 'fa fa-cog fa-spin',
        hide: false,
        addclass: "stack-bottomright",
        stack: stack_bottomright,
        buttons: {
            closer: false,
            sticker: false
        },
        history: {
            history: false
        },
        before_open: function(notice) {
            progress = notice.get().find("div.progress-bar");
            progress.width(cur_value + "%").attr("aria-valuenow", cur_value).find("span").html(cur_value + "%");
            // Pretend to do something.
            var timer = setInterval(loading, 65);
            function loading() {
              if (cur_value == 70) {
                  loader.update({
                      title: "图标嘞图标嘞",
                      icon: "fa fa-circle-o-notch fa-spin"
                  });
                  clearInterval(timer);
                  setTimeout(function () {
                    var timer = setInterval(loading, 65);
                  },13000)
              }
              if (cur_value == 80) {
                  loader.update({
                      title: "图标嘞",
                      icon: "fa fa-refresh fa-spin"
                  });
              }
              if (cur_value == 90) {
                  loader.update({
                      title: "Dividing and conquering",
                      icon: "fa fa-spinner fa-spin"
                  });
              }
              if (cur_value >= 100) {
                  // Remove the interval.
                  window.clearInterval(timer);
                  loader.remove();
                  return;
              }
              cur_value += 1;
              progress.width(cur_value + "%").attr("aria-valuenow", cur_value).find("span").html(cur_value + "%");
          }
        }
    });
}